package org.ruiyichen.guizhou.agr.ui.activity;

import android.os.Bundle;
import android.view.View;

import com.google.gson.reflect.TypeToken;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.base.BaseActivity;
import org.ruiyichen.guizhou.agr.bean.Response;
import org.ruiyichen.guizhou.agr.bean.mode.IIrrigatedDeviceModel;
import org.ruiyichen.guizhou.agr.config.Constant;
import org.ruiyichen.guizhou.agr.net.OkHttpManager;
import org.ruiyichen.guizhou.agr.ui.adapter.DefaultDeviceRecyclerAdapter;
import org.ruiyichen.guizhou.agr.ui.listener.OnRecyclerItemClick;
import org.ruiyichen.guizhou.agr.ui.mineview.BToast;
import org.ruiyichen.guizhou.agr.utils.ListUtil;
import org.ruiyichen.guizhou.agr.utils.StrUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by Administrator on 2018/9/8.
 */

public class DefaultDeviceActivity extends BaseActivity implements View.OnClickListener {
    public static final String TAG_ID = "id";

    private RecyclerView rv_nf_info_root;

    private DefaultDeviceRecyclerAdapter adapter;

    private List<IIrrigatedDeviceModel> list = new ArrayList<>();


    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.df_activity_irrigated_device);
        bOkHttpManager = new OkHttpManager(this, bHandler);
        initData();
    }

    @Override
    protected void initViews() {
        setTitle("默认区域");
        rv_nf_info_root = findViewById(R.id.rv_home_control);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setAutoMeasureEnabled(true);
        rv_nf_info_root.setLayoutManager(manager);
        rv_nf_info_root.setNestedScrollingEnabled(false);
        adapter = new DefaultDeviceRecyclerAdapter(this);
        adapter.setmDatas(list);
        rv_nf_info_root.setAdapter(adapter);
        findViewById(R.id.tv_add_irrigated_area).setVisibility(View.GONE);
    }

    @Override
    protected void initEvents() {
        findViewById(R.id.iv_normal_back).setOnClickListener(this);
        adapter.setOnRecyclerItemClick(new OnRecyclerItemClick() {
            @Override
            public void onItemClick(View view, int position) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_normal_back:
                onBackPressed();
                break;
        }
    }

    private void initData() {
        if (StrUtil.isNotEmpty(getIntent().getStringExtra(TAG_ID))) {
            Map<String, String> body = new HashMap<>();
            body.put("irrigId", getIntent().getStringExtra(TAG_ID));
            bOkHttpManager.httpRequest(Constant.RequestType.GET_DEVICES_BY_IRRIGID, Constant.Http.GET,
                    body, false, new
                            TypeToken<Response<List<IIrrigatedDeviceModel>, Void>>() {
                            }.getType(), true, false);
        }
    }

    @Override
    protected void onPostHandle(int requestType, Object response, boolean error, int errorCode, Object obj1, Object obj2) {
        super.onPostHandle(requestType, response, error, errorCode, obj1, obj2);
        if (error) {
            BToast.getInstance().show(getResources().getString(R.string.abnormal));
        } else {
            if (response == null) {
                BToast.getInstance().show(getResources().getString(R.string.datafailure));
            } else {
                if (requestType == Constant.RequestType.GET_DEVICES_BY_IRRIGID) {
                    Response<List<IIrrigatedDeviceModel>, Void> result = (Response<List<IIrrigatedDeviceModel>, Void>) response;
                    if (result.isSuccess()) {
                        list.clear();
                        if (ListUtil.isNotEmpty(result.getData())) {
                            list.addAll(result.getData());
                        }
                        adapter.notifyDataSetChanged();
                    } else {
                        BToast.getInstance().show(result.getMsg());
                    }

                }
            }
        }
    }

}
